package com.example.dairenkun.neufoodshop.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dairenkun.neufoodshop.R;

import com.example.dairenkun.neufoodshop.activity.EditCommentActivity;
import com.example.dairenkun.neufoodshop.activity.FoodDetailActivity;
import com.example.dairenkun.neufoodshop.bean.Buy;
import com.example.dairenkun.neufoodshop.bean.DelComment;
import com.example.dairenkun.neufoodshop.bean.Food;
import com.example.dairenkun.neufoodshop.bean.Order;
import com.example.dairenkun.neufoodshop.model.FoodModel;
import com.example.dairenkun.neufoodshop.model.OrderModel;
import com.example.dairenkun.neufoodshop.service.CommentService;
import com.example.dairenkun.neufoodshop.service.FoodService;
import com.example.dairenkun.neufoodshop.service.OrderService;
import com.squareup.picasso.Picasso;

import java.util.List;

;import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/3/6.
 */

public class CommentListAdapter extends RecyclerView.Adapter<ViewHolder> {
    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;
    public CommentListAdapter(Context mContext, List mDataList){
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_mycomment, parent, false);
        return new ViewHolder(v);


    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
       final Order entity=(Order) mDataList.get(position);
        if (null == entity)
            return;
        final ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.food_name.setText(entity.getFoodname());
       viewHolder.food_price.setText(entity.getPrice()+"元");
        viewHolder.comment_time.setText(entity.getComment_time());
        viewHolder.commnet_contnet.setText(entity.getContent());
        viewHolder.shop_name.setText(entity.getShopname());
        //viewHolder.food_image.setImageResource(Integer.parseInt(entity.get("food_image").toString()));
           int foodid=entity.getFood_id();
       ////


        //获取网上的数据/
        FoodModel foodModel=new FoodModel();
        //2
        Call call=foodModel.FoodDetail(foodid);
        call.enqueue(new Callback<Food>() {
            @Override
            public void onResponse(Call<Food> call, retrofit2.Response<Food> response)
            {
                Food food=response.body();
                //List  mDataList=food.getTngou();
                if (!"".equals(food.getPic()))
                    Picasso.with(mContext).load(food.getPic()).into(viewHolder.food_image);
            }
            @Override
            public void onFailure(Call<Food> call, Throwable t) {

            }
        });

        //删除
        viewHolder.btn_delect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int orderid=entity.getOrder_id();
                //获取网上的数据/
                OrderModel orderModel=new OrderModel();
                Call call=orderModel.DelComment(orderid);
                call.enqueue(new Callback<DelComment>() {
                    @Override
                    public void onResponse(Call<DelComment> call, retrofit2.Response<DelComment> response)
                    {
                        DelComment order=response.body();
                        String result=order.getSuccess();
                        if("0".equals(result))
                        {
                            //失败处理
                            Toast.makeText(mContext, "删除失败！", Toast.LENGTH_SHORT).show();
                        } else
                        {
                            //登录成功处理
                            Toast.makeText(mContext, "删除成功！", Toast.LENGTH_SHORT).show();
                            //用户Id 的值不对，  sum  值不对
                        }


                    }
                    @Override
                    public void onFailure(Call<DelComment> call, Throwable t) {

                    }
                });
            }
        });
        //修改
        viewHolder.btn_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, EditCommentActivity.class);
                intent.putExtra("order_id",entity.getOrder_id()+"");
                mContext.startActivity(intent);
            }
        });

        //点击进入详情
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,FoodDetailActivity.class);
                intent.putExtra("food_id",entity.getFood_id()+"");
                mContext.startActivity(intent);

            }
        });
    }


    @Override
    public int getItemCount() {
        return mDataList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView food_name;
        TextView food_price;
        Button btn_delect;
        Button btn_edit;
        TextView comment_time;
        TextView shop_name;
        TextView commnet_contnet;
        ImageView food_image;




        public ViewHolder(View itemView) {
            super(itemView);
            food_name = (TextView) itemView.findViewById(R.id.mycomment_foodname);
            food_price = (TextView) itemView.findViewById(R.id.mycomment_money);
            comment_time=(TextView) itemView.findViewById(R.id.mycomment_time);
            btn_delect=(Button)itemView.findViewById(R.id.btn_mycomment_delect);
            btn_edit=(Button)itemView.findViewById(R.id.btn_mycomment_edit);
            shop_name = (TextView) itemView.findViewById(R.id.mycomment_shopname);
            commnet_contnet = (TextView) itemView.findViewById(R.id.mycomment_comment_content);
            food_image=(ImageView) itemView.findViewById(R.id.mycomment_pic);

        }

    }

}
